#include "screenwidget.h"
#include <QLineEdit>

ScreenWidget::ScreenWidget(Screen screen, BaseShowWidget *parent) : BaseShowWidget(parent), m_screen(screen)
{
    //为每一个screen注册点击事件回调函数，通过全局观察者发送消息
    GlobalObserver::getGlobalObserver()->attach(loss_select_msg, this, SLOT(setLossSelect()));

    QVector<Partition> partitions = getSortPartitionsVector(m_screen.partitions);

    for (auto it = partitions.begin(); it != partitions.end(); it++)
        addPartition(*it);
}

QVector<Partition> ScreenWidget::getSortPartitionsVector(QMap<int, Partition> map_partitions)
{
    QVector<Partition> partitions;
    for (auto it = map_partitions.begin(); it != map_partitions.end(); it++)
        partitions.append(it.value());

    qSort(partitions.begin(), partitions.end(),
          [](Partition ele1, Partition ele2){return ele1.common.z_order < ele2.common.z_order;});

    return partitions;
}

QVector<PartitionShowData> ScreenWidget::getSortPartitionWidgetsVector(QMap<int, PartitionShowData> map_partition_widgets)
{
    QVector<PartitionShowData> partition_widgets;
    for (auto it = map_partition_widgets.begin(); it != map_partition_widgets.end(); it++)
        partition_widgets.append(it.value());

    qSort(partition_widgets.begin(), partition_widgets.end(),
          [](PartitionShowData ele1, PartitionShowData ele2){return ele1.partition_data.common.z_order < ele2.partition_data.common.z_order;});

    return partition_widgets;
}

void ScreenWidget::setAllChildrenLossSelect()
{
    setLossSelect();
    for (auto it = m_partitions_data.begin(); it != m_partitions_data.end(); it++)
        it.value().partition_widget->setAllChildrenLossSelect();
}

void ScreenWidget::selectChild(int partition_id)
{
    m_partitions_data[partition_id].partition_widget->setSelected();
}

void ScreenWidget::sortPartitionWidgets()
{
    QVector<PartitionShowData> partition_showdata = getSortPartitionWidgetsVector(m_partitions_data);
    for (auto it = partition_showdata.begin(); it != partition_showdata.end(); it++)
        it->partition_widget->raise();
}

void ScreenWidget::setSelectStyle(bool select)//设置控件选择样式
{
    if (select)
    {
        //setStyleSheet("background-color:rgb(225,255,255);");
        GlobalObserver::getGlobalObserver()->notify(screen_select_msg, m_screen.id);
    }
    else
    {
        //setStyleSheet("border:none;background-color:transparent;");
    }
}

void ScreenWidget::delPartition(int partition_id)
{
    delete m_partitions_data[partition_id].partition_widget;
    m_partitions_data[partition_id].partition_widget = NULL;

    m_screen.partitions.remove(partition_id);
    m_partitions_data.remove(partition_id);
}

void ScreenWidget::addPartition(Partition one_partition)
{
    PartitionShowData one_data;
    one_data.partition_data = one_partition;
    one_data.partition_widget = new PartitionWidget(one_partition, m_screen.id, this);
    m_partitions_data[one_partition.id] = one_data;

    Common this_common = one_partition.common;

    one_data.partition_widget->setGeometry(this_common.x, this_common.y, this_common.width, this_common.height);

    //添加即选中
    one_data.partition_widget->setSelected();
    one_data.partition_widget->show();
}

void ScreenWidget::updateScreen(Screen screen)
{
    m_screen = screen;
    for (auto it = screen.partitions.begin(); it != screen.partitions.end(); it++)
    {
        m_partitions_data[it.key()].partition_data = it.value();
        m_partitions_data[it.key()].partition_widget->updatePartition(it.value());
    }
}
